﻿
using System;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using System.Collections.Generic;
using System.IO;
using ExitGames.Client.Photon;

public class StartUI : MonoBehaviour 
{
    public Button startGame;
    ChessController chessController;
    public Text currRoomPlayer;

    public List<string> currPlays = new List<string>();
    void Awake()
    {
        currRoomPlayer.text = "";
        chessController = GameObject.FindObjectOfType<ChessController>();
        startGame = transform.Find("ready").GetComponent<Button>();
        startGame.onClick.AddListener(() => {
            if (chessController.gameMode==GameMode.StandAlone)
            {
                gameObject.SetActive(false);
                chessController.SetGameState(GameState.Run);
                return;
            }
            if (!PhotonNetwork.IsConnected)
            {
                return;
            }
            if (PhotonNetwork.CurrentRoom.PlayerCount<2)
            {
                return;
            }
            gameObject.SetActive(false);
            chessController.SetGameState(GameState.Run);
        });
        ChessController.ready += ReadyShowUI;
    }

    private void ReadyShowUI()
    {
        gameObject.SetActive(true);

    }
    [PunRPC]
    public void UpdatePlayerList()
    {
        Hashtable table= PhotonNetwork.CurrentRoom.CustomProperties;
        currPlays= (List<string>)table["players"];
        for (int i = 0; i < currPlays.Count; i++)
        {
            currRoomPlayer.text += currPlays[i] + "\n";
        }
    }
        
}